package com.yrot.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yrot.admin.dto.BindUser;
import com.yrot.admin.entity.SysQuestions;
import com.yrot.admin.entity.SysResult;
import com.yrot.admin.entity.SysTrain;
import com.yrot.admin.entity.SysUsers;
import com.yrot.admin.service.ISysQuestionsService;
import com.yrot.admin.service.ISysResultService;
import com.yrot.admin.service.ISysTrainService;
import com.yrot.admin.service.ISysUsersService;
import com.yrot.common.result.R;
import com.yrot.common.task.ScheduledService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hemin
 * @since 2019-04-02
 */
@RestController
@RequestMapping("/mp/train")
public class MpTrainController {

    @Autowired
    ISysTrainService trainService;
    @Autowired
    private ISysUsersService usersService;
    @Autowired
    private ISysResultService resultService;
    @Autowired
    private ISysQuestionsService questionsService;
    @Autowired
    ScheduledService scheduled1;

    @GetMapping("/exam")
    public ModelAndView MpUserLogin(ModelAndView modelAndView){

        //"填写考生信息页面"
        modelAndView.setViewName("/mp/mp-binding");
        modelAndView.addObject("TrainContent","外协单位入厂培训");
        return  modelAndView;
    }
    @GetMapping("/index")
    public ModelAndView MpIndex(ModelAndView modelAndView){

        //"填写考生信息页面"
        modelAndView.setViewName("/mp/mp-index");
        modelAndView.addObject("TrainContent","外协单位入厂学习");
        return  modelAndView;
    }
    /*
     * 未绑定
     */
    @GetMapping("bindUser")
    public ModelAndView bindUser(@Valid BindUser bindUser, BindingResult bindingResult,
                                 ModelAndView modelAndView){
        if(bindingResult.hasErrors()){
            //return R.error(0001,);
            modelAndView.addObject("message",bindingResult.getFieldError().getDefaultMessage());
            modelAndView.setViewName("/mp/prompt-message");
            return modelAndView;
        }
        QueryWrapper<SysResult> trainQueryWrapper = new QueryWrapper<>();
        trainQueryWrapper.eq("user_name",bindUser.getUsername())
                .eq("identifier",bindUser.getIdentifier());
        List<SysResult> list = resultService.list(trainQueryWrapper);
        if(list.size()>0){
            for (SysResult s:list) {
                Integer trainIDBySysResult = s.getTrainId();
                SysTrain byId = trainService.getById(trainIDBySysResult);
                if(byId.getTrainStatus()==1){
                    if(s.getScore()==null){
                        modelAndView.addObject("train",trainService.getById(s.getTrainId()));
                        String content = trainService.getById(s.getTrainId()).getTrainContent();
                        if(content.length()>10){
                            content = content.substring(0,10);
                        }
                        modelAndView.addObject("content",content);
                        modelAndView.setViewName("/admin/train-template");
                        return modelAndView;
                    }else{
                        modelAndView.setViewName("/view/mp/mp-answer");
                        return modelAndView;
                    }
                }
            }
            modelAndView.addObject("message","考生没有待完成的考试！");
        }else{
            //return R.error(0001,"");
            modelAndView.addObject("message","系统中没有该考生！");
        }
        modelAndView.setViewName("/mp/prompt-message");
        return modelAndView;
    }

    /*
     *考试详情
     */
    @GetMapping("examDetails")
    public ModelAndView examDetails(@RequestParam("trainId")String trainId,ModelAndView modelAndView){
        modelAndView.addObject("train",trainService.getById(trainId));
        String content = trainService.getById(trainId).getTrainContent();
        if(content.length()>10){
            content = content.substring(0,10);
        }
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/mp/exam-details");
        return modelAndView;

    }
    /*
     * 跳转页面
     */
    @GetMapping("changePage")
    public ModelAndView userStatus(@RequestParam("url")String url,String message,
                                   ModelAndView modelAndView){

        modelAndView.setViewName(url);
        modelAndView.addObject("message",message);
        return modelAndView;
    }

    /*
     * 跳转答题页面
     */
    @GetMapping("answer")
    public ModelAndView userStatus2(@RequestParam("trainId")String trainId,
                                    @RequestParam("identifier")String identifier,
                                    ModelAndView modelAndView){
        modelAndView.addObject("train",trainService.getById(trainId));
        modelAndView.setViewName("/admin/test");
        modelAndView.addObject("TrainId",trainId);
        modelAndView.addObject("Identifier",identifier);
        return modelAndView;
    }

    /**
     * 查询培训设置
     * @return 培训设置信息
     */
    @GetMapping("/getTrainSet")
    public R getTrainSet(@RequestParam("trainId")int trainId){
        return R.ok(trainService.getById(trainId));
    }
    /**
     * 查询培训人
     * @return 培训人信息
     */
    @GetMapping("/getUser")
    public R getUser(@RequestParam("openId")String openId){
        return R.ok(usersService.getById(openId));
    }
    /**
     * 查询考题
     * @return 考题
     */
    @GetMapping("/getQuestions")
    public List<SysQuestions> getQuestions(@RequestParam("trainId") int trainId) {
        SysTrain train = trainService.getById(trainId);
        int trainNum = train.getTrainNum();
        return questionsService.findRandQuestions(trainId,trainNum);
    }
    /**
     * 培训考题 提交
     * @param answer 答题
     * @return
     */
    @PostMapping("submitAnswer")
    public R getExamSetByExamId(SysResult result,@RequestParam("answer")String answer){
        try{
            return resultService.submitSave(result, answer);
        }catch (Exception e){
            return R.error(0001,"提交失败");
        }
    }
    /**
     * 手动传考试结果
     * @return 考试结果
     */
    @GetMapping("/examALL")
    public void getExamALL() {
        scheduled1.scheduled1();
    }
}
